<?php
require('config.php');
require('WoWCharacterParser.php');
require('readStats.php');
require('custom_stats.php');

$char = new WoWCharacterParser();
$statReader = new StatReader();

$stats = array();
$clean = array();

/* zone/region validation */
if (isset($_GET['z']) && $_GET['z'] == 'eu') {
	$clean['zone'] = 'eu';
} else {
	$clean['zone'] = 'us';
}

/* realm name validation */
if (isset($_GET['r'])) {
	if (preg_match("/^[A-Za-z0-9\\\' -]+$/", $_GET['r'])) {
		$realm = ucwords(strtolower($_GET['r']));
		$realm = trim($realm);
		$realm = str_replace('\\\'', '\'', $realm);
		$realm = str_replace(' ', '+', $realm);
		$clean['realm'] = $realm;
	} else {
		die('Invalid realm name');
	}
} else {
	die('Missing realm name');
}

/* char name validation */
if (isset($_GET['n'])) {
	// check data, should be up to 12, ascii chars, auto format to first up
	if (mb_strlen($_GET['n']) <= 12 && preg_match('/^[A-Za-z0-9]{2,16}$/', $_GET['n'])) {
		$clean['name'] = strtoupper(substr($_GET['n'],0,1)).strtolower(substr($_GET['n'],1));
	} else {
		die('Invalid character name');
	}
} else {
	die('Missing character name');
}

// get the data, from cache/blizzard
if ($char->loadData($clean['zone'], $clean['realm'], $clean['name']) > 0) { // failed
	die('Failed to retrieve data. Please try again.');
}

// parse out the XML
$char->parseXML();

$full_profile = true;
// check for error messages here, or for missing data
if (count($char->character['page'][0]['children']['characterinfo'][0]['children']) == 0) {
	// error xml!1!, exit!
	die('An error has occurred. Check spelling.');
} else if (count($char->character['page'][0]['children']['characterinfo'][0]['children']) == 1) {
		// CHAR DATA AWAITING REFRESH!
		// if missing data, we want to write the cache only if no cache exists
	$char->writeCache($clean['zone'], $clean['realm'], $clean['name'], false); 
	// ideally here, we want to refresh more often than if we got real data!?
	$full_profile = false;
} else {
	// plenty of data, overwrite cache!
	$char->writeCache($clean['zone'], $clean['realm'], $clean['name'], true);
	$full_profile = true;
}

$statReader->setCharObject($char);

// the required stats, that we always need to get!
$required = 'ABCDEFGHIJKMN';

// grab all the required stats first
for ($i = 0; $i < strlen($required); $i++) {
	$stats[$custom_stats[$required[$i]]] = $statReader->getStat($custom_stats[$required[$i]]);
}

if (!$full_profile) {
	echo '<h2>'.$stats['Name'].' &lt;'.$stats['Guild'].'&gt; of '.$stats['Server']."</h2>\n";
	echo 'Level '.$stats['Level'].' '.$stats['Race'].' '.$stats['Class']."<br />\n";

	echo 'Character Data Awaiting Refresh';
	exit;
}

echo '<h2>'.$stats['Name'].' &lt;'.$stats['Guild'].'&gt; of '.$stats['Server']."</h2>\n";
echo 'Level '.$stats['Level'].' '.$stats['Race'].' '.$stats['Class']."<br />\n";
echo $stats['Talent Tree One'].'/'.$stats['Talent Tree Two'].'/'.$stats['Talent Tree Three'].', '.$stats['Build']."<br /><br />\n";

$hash = $default_stats[$stats['Class']][$stats['Build']];
// loop through the stats that we want to extract and get the data
for ($i = 0; $i < strlen($hash); $i++) {
	$stats[$custom_stats[$hash[$i]]] = $statReader->getStat($custom_stats[$hash[$i]]);
	//echo 'hash'.$i.' : '.$hash[$i].' - '.$custom_stats[$hash[$i]].' - '.$stats[$custom_stats[$hash[$i]]].'<br />';
}

for ($i = 0; $i < strlen($hash); $i++) {
	switch ($custom_stats[$hash[$i]]) {
		case 'Secondbar':
			echo $statReader->getStat('Secondbar Type').': '.$stats[$custom_stats[$hash[$i]]]."<br />\n";
			break;
		case 'Prof1 Skill':
			echo $statReader->getStat('Prof1 Name').': '.$stats[$custom_stats[$hash[$i]]]."<br />\n";
			break;
		case 'Prof2 Skill':
			echo $statReader->getStat('Prof2 Name').': '.$stats[$custom_stats[$hash[$i]]]."<br />\n";
			break;
		case '<empty>':
			// do nothing!
			break;
		default:
			echo $custom_stats[$hash[$i]].': '.$stats[$custom_stats[$hash[$i]]]."<br />\n";
	}
}


?>